package com.team7.order.service.impl;

import com.team7.order.dao.ChapterDao;
import com.team7.order.dao.VideoDao;
import com.team7.order.entity.Chapter;
import com.team7.order.entity.Video;
import com.team7.order.service.ChapterService;
import com.team7.order.utils.CommonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.order.dao.ChapterDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 张煜杰
 * @since 2021-10-22
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterDao, Chapter> implements ChapterService {

    @Resource
    private ChapterDao chapterDao;
    @Resource
    private VideoDao videoDao;

    @Override
    public CommonResult findChapterByCourseId(String courseId) {
        List<Chapter> list = chapterDao.findChapterByCourseId(courseId);
        return new CommonResult(2000,"查询成功",list);
    }

    @Override
    public CommonResult findChapterById(String id) {
        //        获取章节

        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        List<Chapter> chapters = chapterDao.selectList(wrapper);

        //      获取课时
        List<Video> videos = videoDao.selectList(null);

        List<Chapter> chapters1 = new ArrayList<>();
        for (Chapter chapter : chapters) {
            Chapter chapter1 = new Chapter();
            chapter1.setTitle(chapter.getTitle());
            chapter1.setSort(chapter.getSort());
            chapter1.setId(chapter.getId());
            List<Video> videos1 = new ArrayList<>();
            for (Video video : videos) {
                if (video.getChapterId().equals(chapter.getId())){
                    videos1.add(video);
                    //TODO
                    chapter1.setVideo(videos1);
                }
            }
            chapters1.add(chapter1);
        }
        for (Chapter chapter2 : chapters1) {
            if (chapter2.getVideo()==null){
                chapter2.setPeriodNum(0);
            }else {
                chapter2.setPeriodNum(chapter2.getVideo().size());
            }
        }
        return new CommonResult(2000,"查询成功",chapters1);
    }

    @Override
    public CommonResult insertChapter( Chapter chapter) {
        if (StringUtils.isEmpty(chapter.getId())){
            //为空则是插入
            int insert = chapterDao.insert(chapter);
            if (insert>0){
                CommonResult commonResult = findChapterById(chapter.getCourseId());
                List<Chapter> result = (List<Chapter>) commonResult.getResult();
                return new CommonResult(2000,"新增章节成功",result);
            }else {
                return new CommonResult(5000,"新增章节失败",null);
            }
        }else {
            //修改该章名称
            int i = chapterDao.updateById(chapter);
            if (i>0){
                CommonResult commonResult = findChapterById(chapter.getCourseId());
                List<Chapter> result = (List<Chapter>) commonResult.getResult();
                return new CommonResult(2000,"修改章节成功",result);
            }else {
                return new CommonResult(5000,"修改章节失败",null);
            }
        }


    }

    @Override
    public CommonResult deleteChapterWithCourseId(Chapter chapter) {
        int i = chapterDao.deleteById(chapter.getId());
        if (i>0){
            CommonResult chapterById = findChapterById(chapter.getCourseId());
            List<Chapter> result = (List<Chapter>)chapterById.getResult();
            for (Chapter chapter1 : result) {
                if (chapter1.getSort()>chapter.getSort()){
                    chapter1.setSort(chapter1.getSort()-1);
                    int i1 = chapterDao.updateById(chapter1);
                    return new CommonResult(2000,"删除章节成功",null);
                }
            }
        }
        return new CommonResult(2000,"删除章节成功",null);
    }

}
